<!DOCTYPE html>
<html lang="en">
<head>    <meta charset="UTF-8">
    <title>购买</title>
    <link href="/others/zui/dist/css/zui.css" rel="stylesheet">
    <script type="text/javascript" src="/others/jquery-2.1.4/jquery.min.js"></script>
    <script type="text/javascript" src="/others/zui/dist/js/zui.js"></script>
    <script type="text/javascript" src="/index.js"></script>
</head>
<body>
    <nav class="navbar navbar-default" role="navigation">
        <div class="collapse navbar-collapse navbar-collapse-example">
            <!-- 一般导航项目 -->
            <ul class="nav navbar-nav" style="width: 100%">
                <li><a href="your/nice/url">主页</a></li>
                <li><a href="your/nice/url">帮助</a></li>
                <li><a href="your/nice/url">购物车</a></li>
                <li><a href="your/nice/url">收藏夹</a></li>
                <li><a href="your/nice/url">联系我们</a></li>

                <li class="top_weather" style="height: 40px;width: 500px;padding-left: 120px">

                </li>
                <li class="collapse navbar-collapse navbar-collapse-example">
                    <!-- 导航中的表单 -->
                    <form class="navbar-form navbar-left" role="search">
                        <div class="form-group">
                            <input type="text" style="width: 400px" class="form-control" placeholder="搜索">
                        </div>
                        <button type="submit" class="btn btn-default">搜索</button>
                    </form>
                </li>
                <ul class="nav navbar-nav" style="float:right" id="login_ul">
                    <button type="button" class="btn btn-lg btn-primary" data-toggle="modal" data-target="#myModal">直接登陆</button>
                    <button type="button" class="btn btn-lg btn-primary" onclick="javascrtpt:window.location.href='./user/html/create.html'">注册</button>
                </ul>
            </ul>
        </div>
    </nav>


    <div class="buy_coms" style="margin: 100px  auto;width: 620px;height: 360px;background-color: #999999">
    </div>


<div class="modal fade" id="myModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">关闭</span></button>
                <h4 class="modal-title">登陆</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal form-condensed" role="form" id="login">
                    <legend>账户登陆</legend>
                    <div class="form-group"><label class="col-md-2 control-label">账户：</label><div class="col-md-4"><input type='text' name='accounts' value='' class="form-control" placeholder="账户："/></div><i class="tishi"></i></div>
                    <div class="form-group"><label class="col-md-2 control-label">密码：</label><div class="col-md-4"><input type='text' name='password' value='' class="form-control" placeholder="密码："/></div><i class="tishi"></i></div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss="modal" onclick="login()">登陆</button>
                <button type="button" class="btn btn-primary" onclick="javascrtpt:window.location.href='./user/html/create.html'">注册</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<script>
    var user_com={
        accounts:window.localStorage.getItem("accounts"),
        password:window.localStorage.getItem("password"),
        id:""
    }
    function parseurl(){
        var thisURL = document.URL
        var  getval =thisURL.split('?')[1];
        var showval= getval.split(/=|&&/)
        user_com.id=showval[1]
    }
    function loding(){
        parseurl()
        send_user(user_com)
        send_com(user_com)
    }
    loding()
    function send_com(com){
        $.ajax({
            type: "POST",
            url: "/app/sc/fincoms",
            data:com,
            success: function(data){
                json_msg=eval('(' + data + ')');
                $(".buy_coms").html(
                        '<div class="com_img_btn" id="'+json_msg.data.id+'" style="width: 300px;height: 300px;text-align: center;float:left">'+
                            '<div class="com_img" style="padding-top: 20px">'+
                                '<img src="/img/comimg/'+json_msg.data.id+'.jpg" alt="'+json_msg.data.name+'" >'+
                            '</div>'+
                            '<p class="com_name" style="padding-top: 8px">'+json_msg.data.name+'</p>'+
                            '<p class="com_stock">'+
                                '<i>库存： </i>'+json_msg.data.stock+
                                '<i>已售： </i>'+json_msg.data.sold+
                            '</p>'+
                        '<p class="com_price">'+
                        '<i>原价： </i>'+(json_msg.data.price*1.2).toFixed(2) +
                        '<i>现价： </i>'+(json_msg.data.price*1).toFixed(2)+
                        '</p>'+
                       '</div >'+
                        '<div id="'+json_user.data[0].accounts+'" style="width: 300px;height: 300px;text-align: center;float:left">' +
                            '<div class="usr_img" style="padding-top: 20px">'+
                                '<img src="/img/usrimg/'+$.trim(json_user.data[0].accounts)+'.jpg" alt="'+json_user.data[0].name+'" style="width: 120px;height: 120px">'+
                            '</div>'+
                            '<div class="users" style="padding-top: 20px">' +
                                '<p><i>用户： </i>'+json_user.data[0].name+'</p>'+
                                '<p><i>栋数： </i>'+json_user.data[0].building+'</p>'+
                                '<p><i>寝室： </i>'+json_user.data[0].dorm+'</p>'+
                                '<p><i>账户余额： </i>'+json_user.data[0].cash+'</p>'+
                            '</div>'+
                        '</div>' +
                        '<span style="width: 100px;height: 30px;text-align: center;padding-left: 230px">'+
                            '<input type="text" id="numbers" value="1" maxlength="8" name="numbers" style="width: 30px">'+
                                '<button class="coms_add" style="width: 26px;height: 26px" onclick="coms_add()">+</button>'+
                                '<button class="coms_reduce" style="width: 26px;height: 26px" onclick="coms_reduce()">-</button>'+
                            '<span >件</span>'+
                            '<span style="padding-left: 30px;color: mediumblue">价格 :  </span>'+
                            '<span id="coms_price" style="color: red">'+(json_msg.data.price*1).toFixed(2)+'</span>'+
                        '</span>'+
                        '<div style="text-align: center">' +
                        '<button class="btn btn-info" onclick="buys()">立即购买</button><button class="btn btn-primary">加入购物车</button>' +
                        '</div>'
                )
            }
        });
    }
    var coms_price
    function coms_add(){
        if(Number(document.getElementById("numbers").value) < Number(json_msg.data.stock)) {
            document.getElementById("numbers").value++
            coms_price=(Number(json_msg.data.price)*Number(document.getElementById("numbers").value)).toFixed(2)
            $('#coms_price').text(coms_price.toString());
        }
    }
    function coms_reduce(){
        if(Number(document.getElementById("numbers").value) > 1) {
            document.getElementById("numbers").value--
            coms_price=(Number(json_msg.data.price)*Number(document.getElementById("numbers").value)).toFixed(2)
            $('#coms_price').text(coms_price.toString());
        }
    }
 function buys(){
      console.log(document.querySelector("#numbers").value)
      console.log(user_com.id)
      user_com.numbers=document.querySelector("#numbers").value
        $.ajax({
            type: "POST",
            url: "/app/sc/buycom",
            data:user_com,
            success: function(data){
                alert(data)
            }
        })
    }
</script>
</body>
</html>
